package com.lagou.server;

import com.lagou.model.Student;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

/**
 * 编程实现服务器的初始化和关闭
 */
public class ServerInitClose {
    /**
     * 自定义成员变量来记录Socket和流对象
     */
    private ServerSocket ss;
    private Socket s;
    private ObjectInputStream ois;
    private ObjectOutputStream oos;

    public ObjectInputStream getOis() {
        return ois;
    }

    public ObjectOutputStream getOos() {
        return oos;
    }
    //学生信息List<Student>
    private List<Student> stuList = null;
    //设置保存学员信息的文件
    private File stuFile = new File("D:\\zz");
    private ObjectOutputStream stuOos = null;
    private ObjectInputStream stuOis = null;
    public List<Student> getStuList() {
        return stuList;
    }
    /**
     * 自定义成员方法实现服务器的初始化操作
     */
    public void serverInit() throws IOException {
        // 1.创建ServerSocket类型的对象并提供端口号
        ss = new ServerSocket(8888);
        // 2.等待客户端的连接请求，调用accept方法
        System.out.println("等待客户端的连接请求...");
        s = ss.accept();
        System.out.println("客户端连接成功！");
        // 3.使用输入输出流进行通信
        ois = new ObjectInputStream(s.getInputStream());
        oos = new ObjectOutputStream(s.getOutputStream());
        System.out.println("服务器初始化成功！");
        //学生信息初始化  初始化stuList
        initStuList();
    }

    /**
     * 初始化学员信息
     */
    private void initStuList() {
        if (stuFile.exists()) {
            try {
                stuOis = new ObjectInputStream(new FileInputStream(stuFile));
                stuList=(List<Student>)stuOis.readObject();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            stuList = new ArrayList<>();
        }
        System.out.println("初始化后，学员信息为："+stuList);
    }

    /**
     * 保存学员信息到文件中D:\zz
     */
    public void saveStuList(){
        try {
            stuOos = new ObjectOutputStream(new FileOutputStream(stuFile));
            stuOos.writeObject(stuList);
            stuOos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 自定义成员方法实现服务器的关闭操作
     */
    public void serverClose() throws IOException {
        // 4.关闭Socket并释放有关的资源
        oos.close();
        ois.close();
        s.close();
        ss.close();
        System.out.println("服务器成功关闭！");
    }
}
